<?php
class UserModel extends BaseModel
{
    public function __construct()
    {
        parent::__construct();
        $this->user_db = new UserDb();
    }

    public function getUserIdByPasskey($passkey)
    {
        $where = array(
            'e' => array(
                'passkey' => $passkey,
            ),
        );
        $column = array(
            'id',
        );
        $r = $this->user_db->selectRow($where, $column);
        if(!empty($r)) {
            return $r['id'];
        }
        throw new ModelException();
    }

    public function updateTraffic($id, $data)
    {
        if(($data['uploaded'] == 0)&&($data['downloaded'] == 0)) {
            return true;
        }
        $where = array(
            'e' => array(
                'id' => $id,
            ),
        );
        $user = $this->user_db->selectRow($where);
        $user['uploaded'] = $user['uploaded'] + $data['uploaded'];
        $user['downloaded'] = $user['downloaded'] + $data['downloaded'];
        return $this->user_db->update($user, $where);
    }
}
?>
